package xyz.weizhi.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.transaction.annotation.Transactional;
import xyz.weizhi.tanhua.dubbo.utils.IdWorker;
import xyz.weizhi.tanhua.dubbo.utils.TimeLineService;
import xyz.weizhi.tanhua.model.mongo.Movement;
import xyz.weizhi.tanhua.model.mongo.MovementTimeLine;
import xyz.weizhi.tanhua.model.vo.PageResult;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author milian
 * @Date 2021/10/25 0025 20:32
 */
@DubboService
public class MovementApiImpl implements MovementApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TimeLineService timeLineService;
    @Override
    public void pubulish(Movement movement) {
        movement.setCreated(System.currentTimeMillis());
        movement.setPid(idWorker.getNextId("Movement"));
        mongoTemplate.save(movement);
        // 保存数据到好友时间线表
        try {
            timeLineService.saveTimeLine(movement.getUserId(), movement.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageResult findByUserId(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId);
            Query query = Query.query(criteria).skip((page-1)*pagesize)
                                                .limit(pagesize)
                                                .with(Sort.by(Sort.Order.desc("created")));
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        long count = mongoTemplate.count(query, Movement.class);
        return new PageResult(page,pagesize,count,list);
    }

    @Override
    public PageResult findByfriendId(Long userId,Integer page, Integer pagesize) {
        Criteria criteria =Criteria.where("friendId").is(userId);
        Query query = Query.query(criteria).skip((page-1)*pagesize)
                                            .limit(pagesize)
                                            .with(Sort.by(Sort.Order.desc("created")));
        List<MovementTimeLine> lines = mongoTemplate.find(query, MovementTimeLine.class);
        long count = mongoTemplate.count(query, MovementTimeLine.class);
        // 提取动态id
        List<ObjectId> movementIds = CollUtil.getFieldValues(lines, "movementId", ObjectId.class);
        Query queryMovement = Query.query(Criteria.where("id").in(movementIds));
        List<Movement> list = mongoTemplate.find(queryMovement, Movement.class);
        return new PageResult(page,pagesize,count,list);
    }

    @Override
    public List<Movement> findByPids(List<Long> idsList) {
        Criteria criteria = Criteria.where("pid").in(idsList);
        Query query = Query.query(criteria);
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return list;
    }

    @Override
    public List<Movement> randomMovements(Integer pagesize) {
        TypedAggregation aggregation = Aggregation.newAggregation(Movement.class,
                Aggregation.sample(pagesize));

        AggregationResults<Movement> movements = mongoTemplate.aggregate(aggregation,Movement.class);

        return movements.getMappedResults();
    }

    @Override
    public Movement findByid(String movementId) {
        return mongoTemplate.findById(movementId, Movement.class);
    }


}
